
const common = {

	//浙里办分享
	zwShare(url,title,content,image){
		if(!url) return;
		ZWJSBridge.share({
			'url':url,
			'title':title||'',
			'content':content||'',
			'image':image||'',
		}).then((result) => {
		console.log(result);
		}).catch((error) => {
		console.log(error);
		})
	},
	//获取图片保存文件名
	getSaveFileName() {
		return Math.random().toString(36).substr(2) + Date.parse(new Date())
	},
	ossHfiveUpload(param, base) {
		let md5 = this.getSaveFileName()

		// let files = param.tempFiles;//文件对象
		let filename = param.name
		let fileurl = param.path; //文件本地地址
		let suffix = filename.substring(filename.lastIndexOf(".") + 1); //获取文件后缀名称
		let file_name = md5 + '.' + suffix //生成最终保存的文件名+后缀
		return new Promise((resolve, reject) => {
			api({
				url: 'Oss/params',
				method: 'POST',
				data: {
					name: file_name,
					md5: md5
				},
				success: (res, code) => {
					let save_file_url = res.data.key
					console.log(res.data.host, fileurl)
					if (base == 'document') {
						var category = {
							'key': res.data.key,
							'policy': res.data.policy,
							'OSSAccessKeyId': res.data.id,
							'signature': res.data.signature,
							'success_action_status': '200',
						}
						var fData = new FormData()
						fData.append('category', category)
						fData.append('key', res.data.key)
						fData.append('policy', res.data.policy)
						fData.append('OSSAccessKeyId', res.data.id)
						fData.append('signature', res.data.signature)
						fData.append('success_action_status', '200')
						fData.append('file', param)
						var xhr = new XMLHttpRequest()
						xhr.open('POST', res.data.host, true)
						xhr.send(fData)
						xhr.onload = function (e) {
							console.log(e)
							if (xhr.status == 200) {
								console.log('success')
								if (save_file_url) {
									resolve(save_file_url)
								}
							}
						}
					} else {
						uni.uploadFile({
							url: res.data.host,
							filePath: fileurl,//要上传文件资源的路径
							name: 'file',
							formData: {
								'key': res.data.key,
								'policy': res.data.policy,
								'OSSAccessKeyId': res.data.id,
								'signature': res.data.signature,
								'success_action_status': '200',
							},
							success: (res) => {
								console.log(res)

							},
							complete: (res) => {
								console.log(res)
								if (res.statusCode) {
									if (save_file_url) {
										resolve(save_file_url)
									}
									return;
								}
							}
						});
					}


				}
			})

		})
		
	},
	getPageUrl() {
        let pages = getCurrentPages();
		let curPage = pages[pages.length-1];
		if(curPage){
			let url = '/'+curPage.route
			let options = curPage.options || curPage.$route.query;
			var page = url + '?'
			for (let key in options) {
				var value = options[key]
				page += key + '=' + value + '&'
			}
			page = page.substring(0, page.length - 1)
			return page;
		}else{
			return '/'
		}
    },
	//获取时间日期
	getDate(type) {
		const date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();

		if (type === 'start') {
			year = year - 60;
		} else if (type === 'end') {
			year = year + 2;
		}
		month = month > 9 ? month : '0' + month;;
		day = day > 9 ? day : '0' + day;
		return `${year}-${month}-${day}`;
	},
}
export default common;
